package scripts;
import bot.ScriptMethods;

public class Otherworldly extends ScriptMethods implements Runnable {

	private int eatAt = 30;
	private int[] drops = { 526, 527, 619, 795, 38, 40, 41, 42, 1277, 1278 };
	private int food = 546;

	public void start() {
		getAccountSettings();
		System.gc();
		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptThread = null;
		scriptRunning = false;
		debug("Stopped script");
	}

	public void run() {
		while (scriptRunning) { try {
			handleEvents();

			if (bankOpen()) {
				withdraw(food, 12);
				wait(800);
				for (int d : drops) {
					depositAll(d);
					wait(random(620, 800));
				}
				closeBank();
			}

			if (getCurrentLevel()[3] <= eatAt && inCombat()) {
				walkTo(ourPlayerX(), ourPlayerY(), false);
				wait(random(600, 800));
			}

			if (invContains(546) && getCurrentLevel()[3] <= eatAt && !inCombat()) {
				useItem(546);
				wait(random(600, 800));
			}
      
			if (getInventorySize() != 30) {
				for (int i = 0; i < getGroundItemCount(); i++) {
					if (inArray(drops, getGroundItemID()[i])) {
						pickupItem(drops);
						wait(random(600, 700));
					} else {
						int[] daaang2 = getNpcIndex(new int[] {298});
						if (daaang2[0] != -1) {
							if (invContains(546) && !bankOpen() && !isSleeping() && !inCombat() && getCurrentLevel()[3] > eatAt) {
								attackNpc(new int[] {298});
								wait(random(800, 1200));
							}
						} else {
							walkTo(173, 3543, false);
							wait(1000);
						}
					}
				}
			}

			int[] banker = getNpcIndex(new int[] {219});
			if (!invContains(546) && !bankOpen()) {
				int[] daaang = getNpcIndex(new int[] {224});
				if (daaang[0] == -1) {
					walkTo(172, 3528, false);
					wait(1000);
				} else {
					openBank();
					wait(2000);
				}
			}

			wait(80);  
		} catch (Exception e) { e.printStackTrace(); } }
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
		setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 85 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
}